﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using DafYomiApp.Resources;

namespace DafYomiApp
{
    using System.Collections;
    using System.Globalization;
    using DafYomiLib;
    using DafYomiPortalLib;
    using Microsoft.Phone.BackgroundAudio;

    public partial class MainPage : PhoneApplicationPage
    {
        private MediaMetadata m_mediaMetadata;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            PopulateComboBoxes();
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private async void PopulateComboBoxes()
        {
            m_mediaMetadata = await MediaMetadata.DownloadMediaMetadataAsync();
            var allRabbis = m_mediaMetadata.AllRabbis();
            Rabanim.ItemsSource = allRabbis;
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
        private void OnMainPageLoaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void OnTractatesSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedTractate = Tractates.SelectedItem as TractateDetails;
            if (selectedTractate == null)
            {
                return;
            }

            IEnumerable<dynamic> pagesList =
                Enumerable.Range(selectedTractate.FirstPageNumber, selectedTractate.LastPageNumber - selectedTractate.FirstPageNumber + 1)
                .OrderBy(a => a)
                .Select(pageNumber => new
                    {
                        PageNumber = pageNumber,
                        Text = Gimatriya.Convert(pageNumber)
                    });
            Pages.ItemsSource = pagesList;
        }

        private async void OnListenButtonClick(object sender, RoutedEventArgs e)
        {
            var selectedRabbi = Rabanim.SelectedItem as Rabbi;
            var selectedTractateDetails = Tractates.SelectedItem as TractateDetails;
            var selectedPage = ((dynamic) Pages.SelectedItem).PageNumber;

            string mediaUri = await MediaPaths.GetMediaUrl(selectedRabbi, selectedTractateDetails.Tractate, selectedPage);

            // Tell the background audio agent to play the current track.
            BackgroundAudioPlayer.Instance.Track = new AudioTrack(null, "SKY.FM", null, null, null, mediaUri, EnabledPlayerControls.Pause);
            BackgroundAudioPlayer.Instance.Volume = 1.0d;
        }

        private void OnRabanimSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedRabbi = Rabanim.SelectedItem as Rabbi;
            if (selectedRabbi == null)
            {
                return;
            }

            Tractates.ItemsSource = ConvertToTractateDetails(m_mediaMetadata.AllTractatesRabbiGivesLessons(selectedRabbi));
        }

        private IEnumerable<TractateDetails> ConvertToTractateDetails(IEnumerable<Tractate> tractates)
        {
            if (tractates == null)
            {
                return null;
            }

            return tractates.Select(tractate => TalmudBavli.TractatesDetails[tractate]);
        }
    }
}